
CV.Tree.Json = new Class({

    initialize: function() {

        this.json = new Request.JSON({
            isSuccess: Function.from(true),
            onSuccess: Function.from(true),
            onError: function(text, error) {
                alert('onError:' + text + ' : ' + error);
            },
            method: 'post',
            url: '/Admin/tree/proxy'
        });
    },
    rename: function(node, newName, oldName) {

        if(newName == oldName) return;

        this.json.get({
            'exec'    : 'rename',
            'id'      : node.data.id,
            'newname' : newName,
            'oldname' : oldName
        });
    },
    remove: function(current) {

        var ids = [];
        current.getChildrenIds(current.children, ids);
        ids.push(current.data.id);

        this.json.get({
            'exec'   : 'remove',
            'ids'   : ids.join(':')
        });
    },
    copy: function(from, to, where, copy, active) {

        var pid = 0;
        switch(where) {
            case 'inside':
                pid = (to.parentNode ? to.data.cid : 0);
                break;
            case 'before':
            case 'after':
                pid = (to.parentNode ? (to.parentNode.name ? to.parentNode.data.cid : 0) : 0);
                break;
        }

        var params =  {
            'exec'  : 'copy',
            'id'    : from.data.id,
            'type'  : from.data.type,
            'pid'   : pid,
            'active': active,
            'where' : where
        };

        new Request.JSON({
            isSuccess: Function.from(true),
            onSuccess: function(json) {
                copy.state.loaded = false;
                copy.state.open = false;
                copy.state.loadable = true;
                copy.children = [];
                copy.tree.$getIndex();
                copy.getDOM('children').innerHTML = '';
                CV.Tree.Draw.update(copy);
                //copy.data = json;
                copy.set({
                    property: {
                        name: from.name + '<b style="color:green;"> (copy)</b>'
                    },
                    data: json
                });
            },
            onError: function(text, error) {
                alert('onError:' + text + ' : ' + error);
            },
            method: 'post',
            url: '/Admin/tree/proxy'
        }).get(params);
    },
    move: function(current, target, where, active) {

        var pid = 0, ids = [];
        switch(where) {
            case 'inside':
                pid = (target.parentNode ? target.data.cid : 0);
                break;
            case 'before':
            case 'after':
                pid = (target.parentNode ? (target.parentNode.name ? target.parentNode.data.cid : 0) : 0);
                break;
        }
        current.getChildrenIds(current.children, ids);

        this.json.get({
            'exec'  : 'move',
            'id'    : current.data.id,
            'pid'   : pid,
            'ids'   : ids.join(':'),
            'active': active,
            'where' : where
        });
    },
    insert: function(node, current, where, type) {

        function success(json) {
            node.data = json;
        }

        var params =  {
            'exec'   : 'insert',
            'cid'    : current.data.cid,
            'active' : current.data.active,
            'name'   : node.name,
            'where'  : where,
            'type'   : type

        };

        new Request.JSON({
            isSuccess: Function.from(true),
            onSuccess: success,
            onError: function(text, error) {
                alert('onError:' + text + ' : ' + error);
            },
            method: 'post',
            url: '/Admin/tree/proxy'
        }).get(params);
    }
});

